package com.slkj.shilixiaoyuanapp.ui.teacher.tool.notice;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.ToolNoticeHistoryListEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.homework.WorkHistoryListActivity;
import com.slkj.shilixiaoyuanapp.view.CustomStateText;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.MySwipeRefreshLayout;
import com.slkj.shilixiaoyuanapp.view.StateLayout;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindColor;
import butterknife.BindView;

import static com.slkj.shilixiaoyuanapp.entity.code.UiCode.PAGE_COUNT;

@ActivityInfo(layout = R.layout.activity_notice_history_list, title = "历史记录",rightHint = "")
public class NoticeHistoryListActivity extends BaseActivity {

    @BindView(R.id.recycer)
    RecyclerView recycerType;
    BaseQuickAdapter adapter;
    @BindView(R.id.layout_root)
    StateLayout stateLayout;
    @BindView(R.id.swipeLayout)
    MySwipeRefreshLayout refreshLayout;
    private int page = 1;
    private int count = PAGE_COUNT;
    List<ToolNoticeHistoryListEntity> toolTaskListEntityList=new ArrayList<>();
    @BindColor(R.color.color_ccc)int colorCcc;
    @BindColor(R.color.colorAccent)int colorAccent;
    @Override
    protected void init() {
        for (int i = 0; i < 20; i++) {
            toolTaskListEntityList.add(new ToolNoticeHistoryListEntity()) ;
        }
        recycerType.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        recycerType.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseQuickAdapter<ToolNoticeHistoryListEntity, BaseViewHolder>(R.layout.item_tool_notice_history, toolTaskListEntityList) {
            @Override
            protected void convert(BaseViewHolder helper, final ToolNoticeHistoryListEntity item) {
                CustomStateText customStateText = helper.getView(R.id.tv_tx);
                customStateText.setStrokeColor(!item.isCanTx()?colorCcc:colorAccent);
                helper.setText(R.id.tv_name,item.getTitle())
                        .setText(R.id.tv_read,item.getRead())
                        .setText(R.id.tv_time,item.getIntime())
                        .setTextColor(R.id.tv_tx,!item.isCanTx()?colorCcc:colorAccent)
                        .setText(R.id.tv_tx,item.getBtName())
                        .getView(R.id.tv_tx)
                        .setOnClickListener(v -> {
                            if (!item.isReadOver()&&!item.isUrge()){
                                HttpHeper.get().toolService().ydtx(item.getId())
                                        .compose(bindToLifecycle())
                                        .compose(getThread())
                                        .subscribe(new CommonCallBack<String>(true, NoticeHistoryListActivity.this) {
                                            @Override
                                            public void onCallBackSuccess(String data) {
                                                LoadSuccessAndFailDialog.showSuccess(NoticeHistoryListActivity.this,data);
                                                item.setBtName(data);
                                                item.setUrge(1);
                                                notifyItemChanged(helper.getAdapterPosition());                                            }
                                        });
                            }
                        });
                helper.itemView.setOnClickListener(v ->
                        NoticeDetailActivity__JumpCenter.builder(NoticeHistoryListActivity.this)
                                .setId(item.getId()).setIsCanTx(item.isCanTx())
                                .setType(NoticeDetailActivity.TYPE_HIS)
                                .create().go());
            }
        };
        adapter.setOnLoadMoreListener(this::loadMore,recycerType);
        recycerType.setAdapter(adapter);
        recycerType.setAdapter(adapter);
        refresh();
        refreshLayout.setOnRefreshListener(this::refresh);
        stateLayout.setOnReLoadListener(() -> refresh());
    }
    private void loadMore() {
        HttpHeper.get().toolService()
                .getNoticeHistory(page+1, count)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<ToolNoticeHistoryListEntity>>( ) {
                    @Override
                    public void onCallBackSuccess(List<ToolNoticeHistoryListEntity> data) {
                        setData(false,data);
                    }

                    @Override
                    public void onFail(String mes) {
                        adapter.loadMoreFail();
                    }
                });
    }
    /**
     * 刷新
     */
    public void refresh() {
        page = 1;
        //这里的作用是防止下拉刷新的时候还可以上拉加载
        adapter.setEnableLoadMore(false);
        HttpHeper.get().toolService()
                .getNoticeHistory(page, count)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<ToolNoticeHistoryListEntity>>( stateLayout) {
                    @Override
                    public void onCallBackSuccess(List<ToolNoticeHistoryListEntity> data) {
                        if (data.isEmpty()){
                            stateLayout.showEmptyView();
                        }
                        setData(true,data);
                        refreshLayout.setRefreshing(false);
                    }
                    @Override
                    public void onFail(String mes) {
                        super.onFail(mes);
                        refreshLayout.setRefreshing(false);

                    }
                });
    }
    private void setData(boolean isRefresh, List data) {
        page++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            adapter.setNewData(data);
        } else {
            if (size > 0) {
                adapter.addData(data);
            }
        }
        if (size < count) {
            //第一页如果不够一页就不显示没有更多数据布局
            adapter.loadMoreEnd(isRefresh);
        } else {
            adapter.loadMoreComplete();
        }
    }
}
